package cn.liontalk.rabbitmq;

import cn.liontalk.common.redis.RedisService;
import cn.liontalk.config.RabbitmqConfig;
import cn.liontalk.entity.MiaoshaMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: 周哲
 * @package: cn.liontalk.rabbitmq
 * @description:
 * @date: 2019/1/23 21:18
 * @version: V1.0
 */
@Service
public class MQsender {

    private static Logger log = LoggerFactory.getLogger(MQsender.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMessage(String message) {
        String result = RedisService.beanToJson(message);
        amqpTemplate.convertAndSend(RabbitmqConfig.QUEUE, result);
    }


    public void sendTopic(Object message) {
        String msg = RedisService.beanToJson(message);
        log.info("send topic message" + msg);
        amqpTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE, RabbitmqConfig.ROUTING_KEY1, msg + "1");
        amqpTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE, RabbitmqConfig.ROUTING_KEY2, msg + "2");
    }


    public void sendMiaoShaMessage(MiaoshaMessage miaoshaMessage) {
        log.info("send miao sha message : " + miaoshaMessage.toString());
        String result = RedisService.beanToJson(miaoshaMessage);
        amqpTemplate.convertAndSend(RabbitmqConfig.EXCHANGE, RabbitmqConfig.ROUTINGKEY, result);
        log.info("successful  message : " + miaoshaMessage.toString());
    }

}
